package bean;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.oreilly.servlet.MultipartRequest;

public class SemiDTO {
	private JoinVo vo;
	private Connection conn;

	public SemiDTO() {
		conn = new MyOracle().getConn();
	}
	
	public ArrayList<JoinVo> select(){
		ArrayList<JoinVo> al = new ArrayList<>();
		String sql = "select * from semi";
		try{
			PreparedStatement pst = conn.prepareStatement(sql);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				
				JoinVo v = new JoinVo();
				v.setMid(rs.getString("mid"));

				al.add(v);
			}
			rs.close();
			pst.close();
			conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}

		return al;
		
		
	}

	public boolean jung() {
		boolean b = false;
		try {
			String sql="select mid from semi where mid=?";
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getMid());
			
			ResultSet rs = pst.executeQuery();
			if(rs.next()) b =true;
			
			pst.close();
			conn.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
	}
	
	public boolean login() {
		boolean b = false;
		try {
			String sql="select mid, pwd from semi where mid=? and pwd=?";
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getMid());
			pst.setString(2, vo.getPwd());
			
			ResultSet rs = pst.executeQuery();
			if(rs.next()) b =true;
			
			pst.close();
			conn.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
	}
	public boolean insert() {
		boolean b = false;
		String sql = "insert into semi(mid,pwd,irum,gender,birth,email,phone,que,job)values(?,?,?,?,?,?,?,?,?)";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getMid());
			pst.setString(2, vo.getPwd());
			pst.setString(3,vo.getIrum());
			pst.setString(4,vo.getGender());
			pst.setString(5,vo.getBirth());
			pst.setString(6, vo.getEmail());
			pst.setString(7,vo.getPhone());
			pst.setString(8,vo.getQue());
			pst.setString(9,vo.getJob());

			int cnt = pst.executeUpdate();
			if (cnt > 0)
				b = true;

			pst.close();
			conn.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return b;

	}
	public boolean delete(){
		boolean b=false;
		
		String sql = "delete from semi where mid=? and pwd=?";
		
		try{
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getMid());
			pst.setString(2, vo.getPwd());
			int cnt = pst.executeUpdate();
			if(cnt>0)b=true;
			
			pst.close();
			conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return b;
		
		
	}
	public boolean update(){
		boolean b=false;
		try{
			String sql="update semi set pwd=?,irum=?,gender=?,birth=?,email=?,phone=?,job=? where mid=?";
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getPwd());
			pst.setString(2,vo.getIrum());
			pst.setString(3,vo.getGender());
			pst.setString(4,vo.getBirth());
			pst.setString(5, vo.getEmail());
			pst.setString(6,vo.getPhone());
			pst.setString(7,vo.getJob());
			pst.setString(8, vo.getMid());

			int cnt = pst.executeUpdate();
			System.out.println(vo.getPwd());
			System.out.println(vo.getIrum());
			System.out.println(vo.getGender());
			System.out.println(vo.getBirth());
			System.out.println(vo.getEmail());
			System.out.println(vo.getPhone());
			System.out.println(vo.getJob());
			System.out.println(vo.getMid());
			
			if(cnt>0){b=true;}
			
			pst.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return b;
	}

	public JoinVo SelectOne(){
		JoinVo v= new JoinVo();
		String sql="select * from semi where mid=?";
		try {
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.setString(1, vo.getMid());
			ResultSet rs=pst.executeQuery();
			if(rs.next()){
				v.setMid(rs.getString("mid"));
				v.setPwd(rs.getString("pwd"));
				v.setIrum(rs.getString("irum"));
				v.setPhone(rs.getString("phone"));
				v.setBirth(rs.getString("birth"));
				v.setGender(rs.getString("gender"));
				v.setJob(rs.getString("job"));
				v.setEmail(rs.getString("email"));
				v.setQue(rs.getString("que"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return v;
	}
	
	public JoinVo getVo() {
		return vo;
	}

	public void setVo(JoinVo vo) {
		this.vo = vo;

	}
	
}
